<?php
class LogCommand extends CConsoleCommand {
	
	private $tempFile;
	private $lastDateDat;
	
	public function actionIndex($type, $limit = 5) {
		//		echo time();
	}
	public function init() {
		set_time_limit ( 0 );
	}
	
	/**
	 * @date 20110405130000
	 */
	
	public function actionAnalyze() {
		$refers =array('http://www.google.com.hk/search?q=hello',
		'http://cn.bing.com/search?q=hello&go=&form=QBLH&qs=n&sk=&sc=8-5',
		'http://www.baidu.com/s?wd=hello',
		
		);
		foreach ($refers as $refer){
			$this->analyze($refer,'hello');
			$this->analyze($refer,'Hello');
		}
	
	}
	public function analyze($refer,$domain) {
		$result = LogCommandHelper::parseReferer ( $refer );
		if ($result ['isEngine']) {
			
			$keywords = $result ['keywords'];
			foreach ( $keywords as $keyword ) {
				$set = array ('domain' => $domain, 'keyword' => $keyword );
				
				echo EngineKeywordStatistic::model ()->getCollection ()->update ( array ('domain' => $domain, 'keyword' => $keyword ), array ('$set' => $set, '$inc' => array ('count' => 1, "hosts.{$result['host']}" => 1 ) )

				, array ('upsert' => true )

				 );
			}
		
		} else {
			
			$set = array ('domain' => $domain, 'refer' => $refer, 'update' => time () );
			
			echo LinkStatistic::model ()->getCollection ()->update ( array ('domain' => $domain, 'refer' => $refer ), array ('$set' => $set, '$inc' => array ('count' => 1 ) )

			, array ('upsert' => true )

			 );
		}
	}
	
	public function __destruct() {
		//				unlink($this->tempFile);
	//		parent :: __destruct();
	}
}